<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background-color:#F7F8FA; }
			.history_search{ padding: 5px 10px; overflow: hidden;}
			.history_search .top b{ float:left; font-size: 12px; font-weight: 400; color: #999; line-height: 35px;}
			.history_search .top a{ float: right;}
			.history_search .top img{ width: 13px; margin-top: 11px;}
			.history_search .list a{ float:left; overflow: hidden; width: 23.5%; margin: 0 2% 2% 0; line-height: 27px; height: 27px; background-color: #fff; font-size: 14px; color: #474747; text-align: center; border-radius: 3px;}
			.history_search .list a:nth-child(4n+4){ margin-right: 0;}
			.display-none{ display:none; }
		</style>
	</head>
	<body>
		<div class="history_search">
			<ul class="top clearfix" id="eliminate">
				<b>历史记录</b>
				<a href="javascript:;" tapmode onclick="eliminaterecord();"><img src="../../image/img_26.png" alt="" /></a>
			</ul>
			<ul class="list clearfix" id="list">
			</ul>
			<script id="template" type="text/template">
			{{ for (var i = 0, len = it.length; i < len; i++) { }}
			<a href="javascript:;" tapmode onclick="keywordSearch(this);">{{=it[i].searchrecord}}</a>
			{{ } }}
			</script>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js" ></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var db;
		var record = [];
		var inputid;
		var eliminate = $api.byId('eliminate');
		apiready = function() {
			api.parseTapmode();
			api.bringFrameToFront({
				from: api.frameName
			});
			opendb();
		}
		//打开数据库
		function opendb() {
			db = api.require('db');
			db.openDatabase({
				name : 'record'
			}, function(ret, err) {
				if (ret.status) {
					initdb();
				}
			});
		}
		//win传递键盘ID
		function setInputid(id)
		{
			inputid = id;
			//alert(inputid+'?');
		}
		//初始化数据库
		function initdb() {
			db.executeSql({
				name : 'record',
				sql : "CREATE TABLE IF NOT EXISTS 'record' ('ID' INTEGER PRIMARY KEY   AUTOINCREMENT,'config_name' VARCHAR(32),'config_value' VARCHAR(255))"
			}, function(ret, err) {
				if (ret.status) {
					Readsearchrecord();
				} else {
					api.alert({
						msg : '初始化失败！请重新打开/n' + JSON.stringify(err),
						buttons : ['确定']
					});
				}
			});
		}


		//读取搜索记录
		function Readsearchrecord() {
			db.selectSql({
				name : 'record',
				sql : "select * from record where config_name='searchrecord' order by ID desc limit 8"
			}, function(ret, err) {
				if (ret) {
					if (ret.data != '') {
						record = [];
						//取出数据库中的数据生成数组
						for (var i = 0, len = ret.data.length; i < len; i++) {
							record.push({
								searchrecord : ret.data[i].config_value
							})
						}
						//倒序输出
						//record.reverse();
						//渲染页面
						$api.removeCls(eliminate, 'display-none');
						UpdateList(record);
					} else {
						//数据库无数据时清除按钮不显示
						$api.addCls(eliminate, 'display-none');
						UpdateList(record);
					}
				} else {
					console.log(JSON.stringify(err))
				}
			})
		};
		//储存搜索关键字
		function Savesearchrecord(keyword) {
			console.log(keyword);
			db.executeSql({
				name : 'record',
				sql : "delete from record where config_name='searchrecord' and config_value='" + keyword + "'"
			});
			db.executeSql({
				name : 'record',
				sql : "insert into record(config_name,config_value) values('searchrecord','" + keyword + "')"
			});
			Readsearchrecord();
		};
		//储存搜索关键字
		function Delsearchrecord(keyword) {
			db.executeSql({
				name : 'record',
				sql : "delete from record where config_name='searchrecord' and config_value='" + keyword + "'"
			});
		};
		//清除搜索记录
		function eliminaterecord() {
			api.confirm({
				title: '系统提示',
				msg: '是否清空历史纪录？',
				buttons: ['取消', '确定']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				if(index == 2)
				{
					db.executeSql({
						name : 'record',
						sql : "delete from record where config_name='searchrecord'"
					});
					$api.addCls(eliminate, 'display-none');
					record = [];
					UpdateList(record);
					api.toast({
						msg : '清除成功'
					});
				}
			});
		};
		//快捷搜索
		function keywordSearch(obj) {
			var keyword = $api.text(obj);
			Savesearchrecord(keyword);
			api.execScript({
				name : 'search_win',
				script : "kuaijieSearch('" + keyword + "','"+inputid+"');"
			});
		}
		//渲染数据
		function UpdateList(record) {
			var act_content = $api.byId('list');
			var tempFn = doT.template($api.byId('template').innerHTML);
			var resultText = tempFn(record);
			$api.html(act_content, resultText);
			api.parseTapmode();
		}
	</script>
</html>